<?php

namespace App\Api\Controllers;

use App\Api\Requests\ConversionRequest;
use App\Models\Conversion;
use App\Models\UserLog;
use Illuminate\Http\Request;

class ConversionsController extends Controller
{

    public function store(Conversion $conversion,ConversionRequest $request){
        //获取比特币实时价格
        $btc_price = \Cache::get('btc_price');
        var_dump($btc_price);die;
		if(!$btc_price){
			return $this->error('btc价格获取失败，请联系客服');
		}
        if($request->btc > $this->user()->btc){
            return $this->error('可用BTC不足');
        }

        $conversion->fill($request->all());
        $conversion->user_id = $this->user()->id;
        $conversion->btc_price = $btc_price;
        $conversion->balance = $btc_price*$request->btc;
        if($conversion->save()){
            //扣减BTC
            (new UserLog())->change(-$conversion->btc,$this->user()->id,'btc',41,'BTC转余额');
            (new UserLog())->change($conversion->balance,$this->user()->id,'balance',16,'BTC转余额');
        }
        return $this->success($conversion);
    }



    public function index(){
        return $this->success($this->user()->conversion()->orderBy('id','desc')->get());
    }
}
